#include <Windows.h>
#include <tchar.h>
#include "resource.h"

int CALLBACK DlgProc(HWND hDlg, UINT nMsg, WPARAM wParam, LPARAM lParam)
{
	switch (nMsg)
	{
		case WM_INITDIALOG:
		{
			SetWindowText(hDlg, _T("Proc2"));

			UINT nWidth		= GetSystemMetrics(SM_CXSCREEN);
			UINT nHeight	= GetSystemMetrics(SM_CYSCREEN);
			RECT rtClient;
			GetClientRect(hDlg, &rtClient);
			SetWindowPos(hDlg, NULL, (nWidth - (rtClient.right - rtClient.left)) / 2, (nHeight - (rtClient.bottom - rtClient.top)) / 2, 0, 0, SWP_NOSIZE);
			
			break;
		}
		case WM_COMMAND:
		{
			switch (LOWORD(wParam))
			{
				case IDC_BTNVIEW:
				{
					TCHAR* szVal = NULL;
					DWORD nValLen = GetEnvironmentVariable(_T("proc1"), szVal, 0);
					if (0 != nValLen)
					{
						szVal = new TCHAR[nValLen + 1];
						GetEnvironmentVariable(_T("proc1"), szVal, nValLen);
						SetDlgItemText(hDlg, IDC_STCINFO, _T("I see proc1 progrom!"));
						HANDLE hProc1 = (HANDLE)_ttol(szVal);
						delete[] szVal;
						WaitForSingleObject(hProc1, INFINITE);
						SetDlgItemText(hDlg, IDC_STCINFO, _T("Proc1 progrom exited!"));		
						Sleep(1000);
											
						PostQuitMessage(0);

					}
				}
			}
			break;
		}
		case WM_CLOSE:
		{
			PostQuitMessage(0);
			break;
		}
	}

	return 0;
}

BOOL WINAPI _tWinMain(HINSTANCE hInstance, HINSTANCE hPreInstance, LPTSTR szCmdLine, int nShowCmd)
{
	DialogBox(hInstance, MAKEINTRESOURCE(IDD_DIALOG), NULL, DlgProc);

	return TRUE;
}